//监听商品类目
$("#pointLabelName").click(function(){
    layer.open({
        type: 1,
        title: "选择",
        area: ['28%', '60%'],
        content: $(".showChooseDiv"),
        maxmin: false,
        shadeClose: true,
        shade: false,
    });
});
function getData(){
    var data = [];
    $.ajax({
        url : '/item/cat/list',//后台数据请求地址
        type : 'post',
        async : false,
        success: function(resut){
            data = resut;
        }
    });
    return data;
}
var jsonData = JSON.parse(JSON.stringify(getData()));
var formatData = [];
for(var item in jsonData){
    if(jsonData[item].pid === 0){
        var tempObject = {};
        tempObject.title = jsonData[item].name;
        tempObject.id = jsonData[item].id;
        tempObject.children = getChildren(tempObject.id);
        formatData.push(tempObject);
    }
}
function getChildren(id){    //递归体  即对每条data逐条递归找children
    var tempArray = [];
    for(var i in jsonData){
        if(jsonData[i].pid === id){
            var tempChild = {};
            tempChild.title = jsonData[i].name;
            tempChild.id = jsonData[i].id;
            if(selectChildren(jsonData[i].id)){   //若存在子节点，继续递归；否则为叶节点，停止递归
                tempChild.children = getChildren(jsonData[i].id);
            }
            tempArray.push(tempChild);
        }
    }
    return tempArray;
}
function selectChildren(id){   // 是否存在子节点
    for(var i in jsonData){
        if(jsonData[i].pid === id){
            return true;
        }
    }
    return false;
}
tree.render({
    elem: '#dept_tree',
    data: formatData,
    id: 'treeId', //标识
    showCheckbox: false, //是否显示复选框
    onlyIconControl: true,
    click: function (obj) {
        $("[name='cid']").val(obj.data.id);
        $("[name='cidName']").val(obj.data.title);
        layer.close(layer.index);
    }
});